package notice

import (
	"bytes"
	"engine/dto"
	"fmt"
	"github.com/goccy/go-json"
	"lib/config"
	"lib/log"
	"net/http"
)

func SendWechatMsg(msg string, token string) {
	data := map[string]any{
		"msgtype": "text",
		"text": map[string]string{
			"content": fmt.Sprintf("[%s] %s", config.GetSystemConfig().Mode, msg),
		},
	}
	jsonReq, _ := json.Marshal(data)
	req, _ := http.NewRequest("POST",
		fmt.Sprintf("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=%s", token),
		bytes.NewBuffer(jsonReq),
	)
	http.DefaultClient.Do(req)
}

func SendWechatMsgf(wechatKey string, msgFormat string, args ...any) {
	SendWechatMsg(fmt.Sprintf(msgFormat, args...), wechatKey)
}

// SendTechWechatGroupMessage 发送技术群消息  https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=de06ea1a-f0a9-4ac7-b712-50267ae0b1b9
func SendTechWechatGroupMessage(messageFormat string, args ...any) {
	jsonData, err := dto.GetSystemConfiguration("tech-wechat-group-id")
	if err != nil {
		log.Errorf("获取技术群ID失败：%v", err)
		return
	}
	SendWechatMsg(fmt.Sprintf(messageFormat, args...), jsonData.String())
}
